﻿using System;
using System.Data;

using BLL;
using Model;
using Utility;

namespace EvaluationSystem
{
    public partial class AddActivity : System.Web.UI.Page
    {
        private string logonUserName = string.Empty;
        private string enterpriseFullName = string.Empty;
        private string users_id = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            ((SiteSystemMaster)Master).InitMenu();
            ((SiteSystemMaster)Master).li1.Attributes.Add("class", "hover ccc");
            ((SiteSystemMaster)Master).b_hmenu1.Style.Clear();
            ((SiteSystemMaster)Master).b_hmenu2.Style.Add("display", "none");
            ((SiteSystemMaster)Master).b_hmenu3.Style.Add("display", "none");
            ((SiteSystemMaster)Master).s_menu1_1.Style.Add("COLOR", "#147ab8");
            ((SiteSystemMaster)Master).s_menu1_1.Style.Add("TEXT-DECORATION", "none");
            btnSave.Attributes.Add("onclick", "return Check();");
            /*判断是否是管理员身份*/
            if (Session["username"] == null)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "PopupScript", "<script>alert('请先登录系统!');top.location.href='../System/Login.aspx';</script>");
                return;
            }
            else
            {
                if (!IsPostBack)
                {
                    logonUserName = Request.QueryString["users_name"];
                    enterpriseFullName = Request.QueryString["users_incname"];
                    users_id = Request.QueryString["users_id"];

                    lblEnterpriseName.Text = logonUserName;
                    lblEnterpriseFull.Text = enterpriseFullName;
                    txtbActivityenterpriseuid.Text = users_id;

                    bind();
                }
            }
        }

        private void bind()
        {
            if (string.IsNullOrEmpty(users_id))
            {
                return;
            }

            PositionBLL positionBll = new PositionBLL();
            DataSet dsPosition = positionBll.GetPositionAll("");
            ddlPosition.DataSource = dsPosition.Tables[0].DefaultView;
            ddlPosition.DataTextField = "position_name";
            ddlPosition.DataValueField = "position_id";

            ddlPosition.DataBind();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtbActivityenterpriseuid.Text))
            {
                MessageBox.Show("请先选择一个企业用户，然后再添加相应的项目.");
                return;
            }

            if (string.IsNullOrEmpty(txtbActivity.Text.Trim()))
            {
                MessageBox.ShowAndRedirect(this, "测评项目添加失败，请输入正确的活动名称!",
                            "ActivityList.aspx?users_id=" + txtbActivityenterpriseuid.Text +
                            "&users_name=" + lblEnterpriseName.Text +
                            "&users_incname=" + lblEnterpriseFull.Text);
                return;
            }

            ActivityBLL activityBll = new ActivityBLL();

            Activity temp = new Activity();

            temp.Activity_enterpriseuid = int.Parse(txtbActivityenterpriseuid.Text);
            temp.Activity_name = txtbActivity.Text;
            temp.Activity_positionid = int.Parse(ddlPosition.SelectedValue);
            temp.Activity_status = 1;
            DateTime dtCreateTime = DateTime.Now;
            temp.Activity_createtime = dtCreateTime;
            //temp.Activity_holdtime = DBNull.Value ;
            
            temp.Activity_welcometext = "欢迎您参加 【公司名】 测评！";
            temp.Activity_companyintrotext = "ＸＸＸ集团是经销奔驰、别克、凯迪拉克等多种品牌的集团经销商，资金雄厚，管理规范，是有志之士职业成长的平台";
            temp.Activity_content = "本次招聘活动希望通过科学的测评广纳人才，为新开设的多家经销店输入适合招聘岗位的新鲜血液";

            temp.Activity_viewreport = 2111111;//新建测评活动时，默认不允许用户查看报表
            
            int nAddResult = activityBll.Add(temp);

            if (nAddResult != -1)
            {
                Activity activity = activityBll.GetModel(nAddResult);
                if (activity != null)
                {
                    int activityID = nAddResult;

                    //添加活动时，根据岗位设置相应的默认维度>>>>>>>>>>>>>>>>>开始
                    int posID = int.Parse(ddlPosition.SelectedValue);
                    DimensionBLL dimBll = new DimensionBLL();
                    DataSet dimDS = dimBll.GetDefaultDimensionByPosID(posID.ToString());
                    if (dimDS != null && dimDS.Tables[0].Rows.Count > 0)
                    {
                        for (int i = 0; i < dimDS.Tables[0].Rows.Count; i++)
                        {
                            string dimNM = dimDS.Tables[0].Rows[i]["dimension_name"].ToString().Trim();
                            if (dimNM.Equals("社会称许性") || dimNM.Equals("心理健康度") || dimNM.Equals("诚信"))
                            {
                                int rows = dimBll.SetActivityDimension(activityID,
                                    Int32.Parse(dimDS.Tables[0].Rows[i][0].ToString()),
                                    0);//三个特殊维度权重为0
                            }
                            else
                            {
                                int rows = dimBll.SetActivityDimension(activityID,
                                    Int32.Parse(dimDS.Tables[0].Rows[i][0].ToString()),
                                    (float)1.0 / (dimDS.Tables[0].Rows.Count - 3));//减去3个特殊维度后，取平均值。
                            }
                        }
                    }
                    //添加活动时，根据岗位设置相应的默认维度<<<<<<<<<<<<<<<<<结束

                    MessageBox.ShowAndRedirect(this, "测评项目添加成功！",
                "ActivityList.aspx?users_id=" + txtbActivityenterpriseuid.Text +
                "&users_name=" + lblEnterpriseName.Text +
                "&users_incname=" + lblEnterpriseFull.Text);
                }
                else
                {
                    System.Diagnostics.Debug.Assert(false);
                }
            }
            else
            {
                //Todo:
                MessageBox.ShowAndRedirect(this, "测评项目添加失败！",
             "ActivityList.aspx?users_id=" + txtbActivityenterpriseuid.Text +
             "&users_name=" + lblEnterpriseName.Text +
             "&users_incname=" + lblEnterpriseFull.Text);
            }
        }

        protected void btnCancle_Click(object sender, EventArgs e)
        {
            Response.Redirect("ActivityList.aspx");
        }
    }
}